package domain

import (
	  "../db"
	"fmt"
)

type Goods struct {
	Id       string `json:"id"`
	Name     string `json:"name"`
	Price    float64 `json:"price"`
	Off      float64 `json:"off"`
	Picture  string `json:"picture"`
	Category int `json:"category"`
	Barcode  string `json:"barcode"`
	PictureUrl string  `json:"picture_url"`
}

const KEY_PREFIS = "goods/"
func GetGoods(barcode string) (g Goods,ok bool) {
	//db, _ := sql.Open("mysql", "root:root@tcp(localhost:3306)/Market")

	stmt, _ := db.MysqlDb.Prepare("SELECT  id,name,price,off,picture,category,barcode FROM goods WHERE barcode = ?")
	rs, err := stmt.Query(barcode)

	if err == nil && rs.Next() {
		err = rs.Scan(&g.Id, &g.Name, &g.Price, &g.Off, &g.Picture, &g.Category, &g.Barcode)
		rs.Close()
		ok = true
	}else {
		ok = false
	}
	return
}

func AddGoods(g Goods) (affectNum int64,ok bool) {
	stmt, _ := db.MysqlDb.Prepare("INSERT  INTO goods(id, name, price, off, picture, category, barcode)  VALUES (?,?,?,?,?,?,?)")
	fmt.Println(g)
	rs,err := stmt.Exec(g.Id,g.Name,g.Price,g.Off,g.Picture,g.Category,g.Barcode)
	if err == nil{
		affectNum,_ = rs.RowsAffected()
		ok = true
	}else {
		fmt.Println(err)
		affectNum = 0
		ok = false
	}
	return
}
